﻿using AntMovement.Core.Entity;
using AntMovement.Core.Logic.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.PCI9016.Logic
{
    public class Coil : ICoil
    {
        private DO _do;
        public Coil(DO dO)
        {
            _do = dO;
        }

        public bool Status => _do.Status;

        public string Execute(bool bl)
        {
            if (_do == null)
            {
                throw new Exception("对象为空");
            }
            if (bl == _do.Status)
            {
                return "ok";
            }
            long error = PCI9016.p9016_set_do_bit(_do.CardNo, (uint)((_do.MdlIndex * 16) + _do.OrderlyNum), (uint)(bl ? 1 : 0));
            if (error == 0)
            {
                return "ok";
            }
            throw new Exception($"CardNo[{_do.CardNo}] Index[{((_do.MdlIndex * 16) + _do.OrderlyNum)}] p9016_set_do_bit ==>> {(PCI9016Error)error}");
        }
    }
}
